﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System.Windows;
using System.Windows.Data;
using System.Windows.Controls;
using System.Text.RegularExpressions;
using FinPlusInterfaces;

namespace FinPlusCompView
{
    public class FinPlusGridColFormat : FinPlusGridColBase
    {
        [State(Test = true)]
        public override bool IsValid { get; set; }

        //construct
        public FinPlusGridColFormat(string name, string stringFormat, string inputMask, string type, CheckBox filterCheck, bool hasFilter = false,  bool isEnabled = false)
        {
            var tb = new FrameworkElementFactory(typeof(FinPlusValueFormatTextBox));
            tb.SetBinding(FinPlusValueFormatTextBox.IdProperty, new Binding(BeanField.Id.ToString()) { Mode = BindingMode.OneWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });
            tb.SetBinding(FinPlusValueFormatTextBox.ValueProperty, new Binding(BeanField.Type.ToString()) { Mode = BindingMode.OneWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });
            tb.SetBinding(FinPlusValueFormatTextBox.TextProperty, new Binding(name) { StringFormat = stringFormat, Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });
            tb.SetValue(FinPlusValueFormatTextBox.IsEnabledProperty, isEnabled);
            tb.SetValue(FinPlusValueFormatTextBox.StringFormatProperty, stringFormat);
            tb.SetValue(FinPlusValueFormatTextBox.TypeProperty, type);
            tb.SetValue(FinPlusValueFormatTextBox.InputMaskProperty, string.IsNullOrEmpty(inputMask) ? null : new Regex(inputMask));
            var et = new DataTemplate() { VisualTree = tb };

            var dt = TextBlockCell(name, stringFormat, type);
      
            Header = hasFilter ? HeaderFilterCheck(name, filterCheck) : (object)name;
            CellEditingTemplate = et;
            CellTemplate = dt;
            ClipboardContentBinding = new Binding(name) { Mode = BindingMode.OneWay };
            SortMemberPath = name;
            CanUserSort = true;
        }
    }
}
